package com.hshx.controller;

import com.hshx.client.UsersClient;
import com.hshx.pojo.Users;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@RequestMapping("/workService")
@RestController
public class TestController {

    @Autowired
    private UsersClient usersClient;
    @RequestMapping("/test")
    public List<Users> test(){
        System.out.println("远程调用测试");
        return usersClient.getUsersList();
    }

    @RequestMapping("/testSession")
    public ModelAndView testSession(HttpSession session, String uname,
                                    RedirectAttributes ra){
//        192.168.203.1
        ModelAndView mav=new ModelAndView("redirect:http://192.168.203.1:9001/workService/session");
        System.out.println("消费者》sessionId:"+session.getId());
//        Emp emp=new Emp();
//        emp.setEid(1);
//        session.setAttribute("emp",emp);

//        session.setAttribute("user","uname");
//        if(session.getAttribute("emp")==null){
//            System.out.println("此处的session为null");
//        }else{
//            System.out.println(((Emp) session.getAttribute("emp")).toString());
//        }
//        session.setAttribute("user","uname");
        ra.addAttribute("token","token-123");
        return mav;
    }

    @ResponseBody
//解决第一次访问的sessionId不同的方法
    @RequestMapping("/session")
    public String testSession2(HttpSession session,String token){
        System.out.println(session.toString());
        System.out.println("周转方法:"+token);
        String s = usersClient.testSession();
        Users users=new Users();
        users.setUsers_id(1);
        session.setAttribute("users",users);

        System.out.println("提供者返回："+s);
        if(session.getAttribute("users")==null){
            System.out.println("此处的session为null");
        }else{
            System.out.println(((Users) session.getAttribute("users")).toString());
        }
//        Emp newEmp= (Emp) session.getAttribute("emp");
//        System.out.println(newEmp.toString());
        return "消费者sesssionId:"+session.getId()+"====提供者:"+s;
    }

    @GetMapping("/test3")
    public void test3() {
        NoticeWebsocket.sendMessage("你好，WebSocket");
//        return R.ok();
    }

    @GetMapping("/test2")
    public void test2() {
        NoticeWebsocket.sendMessage("各单位注意一下,下午3:35有个会议!");
//        return R.ok();
    }
}
